package json;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2020/10/21 19:06
 */
public class ReadExcelTest {
    public static void main(String[] args) throws IOException {
        //读取具体的一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("test.xls")));
        //拿到第一张表
        HSSFSheet sheet = workbook.getSheetAt(0);
        //拿到第一行
        HSSFRow row0 = sheet.getRow(0);
        int lastCellNum = (int)row0.getLastCellNum();

        //打印第一行
        for (int i = 0; i < lastCellNum; i++) {
            System.out.print(row0.getCell(i).getStringCellValue()+"\t");
        }
        System.out.println();
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum-1; i++) {
            HSSFRow row = sheet.getRow(i);
            HSSFCell cell0 = row.getCell(0);
            HSSFCell cell1 = row.getCell(1);
            HSSFCell cell2 = row.getCell(2);

            System.out.print((int)cell0.getNumericCellValue()+"\t");
            System.out.print(cell1.getStringCellValue()+"\t");
            System.out.print(cell2.getNumericCellValue());
            System.out.println();
        }

        //System.out.println();
        //打印最后一行
        //System.out.println(sheet.getLastRowNum());
        HSSFRow row1 = sheet.getRow(lastRowNum);
        System.out.print(row1.getCell(0).getStringCellValue()+"\t");
        System.out.println(row1.getCell(2).getNumericCellValue());

        HSSFRow row2 = sheet.getRow(lastRowNum-1);
        System.out.print(row2.getCell(0).getStringCellValue()+"\t");
        System.out.println(row2.getCell(2).getNumericCellValue());





    }
}
